import { useState } from "react";

/**
 * 操作数组的hook
 * @param {T[]} value
 * @returns {ReturnProps<T>}
 */
export const useArray = <T>(value: T[]) => {
  const [arr, setValue] = useState(value);
  const removeIndex = (index: number) => {
    const copy = [...arr];
    copy.splice(index, 1);
    setValue(copy);
  };
  const pushIndex = (val: T, index?: number) => {
    const copy = [...arr];
    copy.splice(index ?? 0, 0, val);
    setValue(copy);
  };
  const add = (val: T) => {
    setValue([...arr, val]);
  };
  const clear = () => {
    setValue([]);
  };
  return {
    arr,
    removeIndex,
    pushIndex,
    add,
    clear,
  };
};
